﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using RkLib.Xml;
using RkLib.Configuration;
using RkLib;
using System.IO;
namespace Ebenalp
{
    class ItemInfo
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public string SN { get; set; }
        public ItemInfo(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }
    class SnGen
    {
        private const string XML_CAT = "/root/catalog/cat";
        private const string XML_NAME = "name";
        private const string XML_ABBR = "abbr";
        private const int ITEM_COUNT = 10;

        public const int ITEM5 = 4;
        public const int ITEM7 = 6;
        public const int ITEM9 = 8;

        private ItemInfo[] ArrItems = new ItemInfo[ITEM_COUNT];

        private XmlManager XmlMgr = null;

        public string[] Item7Sub{protected set;get;}

        public SnGen(XmlManager xmlMgr)
        {
            XmlMgr = xmlMgr;
            Item7Sub = GetSumItems(ITEM7);
        }
        public ItemInfo this[int index]
        {
            get
            {
                if (index < ITEM_COUNT)
                {
                    return ArrItems[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (index < ITEM_COUNT)
                {
                    ArrItems[index] = value;
                }
            }
        }
        public override string ToString()
        {
            string str = "";
            for (int i = 0; i < ITEM_COUNT; i++)
            {
                if (ArrItems[i] != null)
                {
                    str += ArrItems[i].SN;
                }
            }
            return str;
        }

        public void ValueChanged(int index, string name, string value)
        {
            ItemInfo info = ArrItems[index];
            info.Name = name;
            info.Value = value;
            if (info.Value == "")
            {
                info.SN = "";
            }
            else
            {
                GetSubSn(index);
            }
        }

        private void GetSubSn(int index)
        {
            string str;
            ItemInfo info = ArrItems[index];
   
            XmlMgr.Source = XML_CAT;
            XmlNodeList NodCatLst = XmlMgr.Nodes;
            XmlNode NodCat = NodCatLst[index];
            XmlNodeList NodLst = NodCat.ChildNodes;

            if (ITEM5 == index)
            {
                str = PublicFunction.GetNumStrFromStr(info.Value);
                if (str != "")
                {
                    info.SN = string.Format("{0:d3}", Convert.ToInt32(str));
                }
            }
            else if (ITEM7 == index)
            {
                foreach (XmlNode node in NodLst)
                {
                    if (node.Attributes[0].Value == info.Name)
                    {
                        XmlNodeList NodLst2 = node.ChildNodes;
                        foreach (XmlNode nod2 in NodLst2)
                        {
                            XmlMgr.Current = nod2;
                            str = XmlMgr.getValue(XML_NAME);
                            if (str == info.Value)
                            {
                                info.SN = XmlMgr.getValue(XML_ABBR);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            else if (ITEM9 == index)
            {
                str = PublicFunction.GetDoubleStrFromStr(info.Value);
                info.SN = str.Replace(".", "");
                if (ArrItems[ITEM7].Name == Item7Sub[0])
                { 
                    if (Convert.ToDouble(str)<10)
                    {
                        if (info.SN.Length ==1)
                        {
                            info.SN = "0" + info.SN + "0";
                        }
                        if (info.SN.Length > 1)
                        {
                            info.SN = "0" + info.SN.Substring(0, 2);
                        }
                    }
                    else
                    {
                        info.SN = info.SN.Substring(0, 2) + "0";
                    }
                }
                else
                {
                    if (Convert.ToDouble(str)<10)
                    {
                        if (info.SN.Length == 1)
                        {
                            info.SN = "0" + info.SN + "0";
                        }
                        if (info.SN.Length > 1)
                        {
                            info.SN = "0" + info.SN.Substring(0, 2);
                        }
                    }
                    else
                    {
                        info.SN = info.SN.Substring(0, 2);
                    }
                }
            }
            else
            {
                foreach (XmlNode nod in NodLst)
                {
                    XmlMgr.Current = nod;
                    str = XmlMgr.getValue(XML_NAME);
                    if (str == info.Value)
                    {
                        info.SN = XmlMgr.getValue(XML_ABBR);
                        return;
                    }
                }
            }
        }
        public string[] GetSumItems(int index)
        {
            string[] arr;
            XmlMgr.Source = XML_CAT;
            XmlNodeList NodCatLst = XmlMgr.Nodes;
            XmlNode NodCat = NodCatLst[index];
            XmlNodeList NodLst = NodCat.ChildNodes;
            if (NodLst.Count == 0)
            {
                return null;
            }
            arr = new string[NodLst.Count];
            for (int i = 0; i < NodLst.Count;i++ )
            {
                arr[i] =  NodLst[i].Attributes[XML_NAME].Value;
            }
            return arr;
        }
        public string[] GetSumItemsName(int index,string parent = null)
        {
            string[] arr = null;
            XmlMgr.Source = XML_CAT;
            XmlNodeList NodCatLst = XmlMgr.Nodes;
            XmlNode NodCat = NodCatLst[index];
            XmlNodeList NodLst = NodCat.ChildNodes;
            if (NodLst.Count == 0)
            {
                return null;
            }
            if (null != parent)
            {
                foreach (XmlNode node in NodLst)
                {
                    if (node.Attributes.Count>0 && node.Attributes[0].Value == parent)
                    {
                        XmlNodeList Lst = node.ChildNodes;
                        if (Lst.Count>0)
                        {
                            arr = new string[Lst.Count];
                            for (int i = 0; i < Lst.Count; i++)
                            {
                                XmlMgr.Current = Lst[i];
                                arr[i] = XmlMgr.getValue(XML_NAME);
                            }
                        }
                        break;
                    }
                }
            }
            else
            {
                arr = new string[NodLst.Count];
                for (int i = 0; i < NodLst.Count; i++)
                {
                    XmlMgr.Current = NodLst[i];
                    arr[i] = XmlMgr.getValue(XML_NAME);
                }
            }

            return arr;
        }

        public void ImportPicture(string strPath)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(strPath);
            foreach (var file in dirInfo.GetFiles())
            {
                if (Path.GetExtension(strPath).ToLower() == ".jpg")
                {
                    Cfg.WriteConfig(gVar.CFG_PIC,Path.GetFileNameWithoutExtension(strPath), file.FullName);
                }
            }
        }
        public void ImportPicture(string[] files)
        {
            foreach (var str in files)
            {
                Cfg.WriteConfig(gVar.CFG_PIC, Path.GetFileNameWithoutExtension(str), str);
            }
        }
    }
}
